package com.platform.api.core.config;

import cn.hutool.core.collection.CollectionUtil;
import com.platform.core.consts.CommonConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

/**
 * swagger配置
 *
 * 加入分组功能(默认注释掉)
 * <p>
 * https://doc.xiaominfo.com/knife4j/changelog/2017-12-18-swagger-bootstrap-ui-1.7-issue.html
 * </p>
 * @author ldw4033#163.com
 * @date 2021/4/9 10:42
 **/
@Configuration
@EnableSwagger2WebMvc
public class OpenApiSwaggerConfig {

    private List<Parameter> getParameters() {
        Parameter parameter = new ParameterBuilder()
                .name("Authorization")
                .description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        List<Parameter> parameters = CollectionUtil.newArrayList();
        parameters.add(parameter);
        return parameters;
    }

//    @Bean
//    public Docket defaultApi() {
//        List<Parameter> parameters = getParameters();
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(defaultApiInfo())
//                .groupName("默认接口")
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(CommonConstant.DEFAULT_PACKAGE_NAME))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(parameters);
//    }
//
//    private ApiInfo defaultApiInfo() {
//        return new ApiInfoBuilder()
//                .title("长久集团 Doc")
//                .description("长久集团 Doc文档")
//                .termsOfServiceUrl("https://www.baidu.com")
//                .contact(new Contact("gyp", "https://gitee.com/", ""))
//                .version("1.0")
//                .build();
//    }

     @Bean
     public Docket groupRestApi() {
         List<Parameter> parameters = getParameters();
         return new Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(groupApiInfo())
                 .groupName("开放接口")
                 .select()
                 //TODO 这里改为自己的包名
                 .apis(RequestHandlerSelectors.basePackage("com.platform.api"))
                 .paths(PathSelectors.any())
                 .build()
                 .globalOperationParameters(parameters);
     }

     private ApiInfo groupApiInfo() {
         return new ApiInfoBuilder()
                 .title("开放接口")
                 .description("开放接口,配置完成之后，用户可进行访问")
                 .termsOfServiceUrl("http://www.example.com/")
                 .version("1.0")
                 .build();
     }

}
